package cn.gson.boot.controller;

import cn.gson.boot.common.MD5Util;
import cn.gson.boot.model.pojos.Dept;
import cn.gson.boot.model.pojos.Role;
import cn.gson.boot.model.pojos.User;
import cn.gson.boot.model.service.EmpRoleService;
import cn.gson.boot.model.service.EmpService;
import cn.gson.boot.model.service.LoginService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("empp")
public class EmpController {
    @Autowired
    EmpService empService;
    @Autowired
    EmpRoleService empRoleService;

    //查询所有员工
    @RequestMapping("/empp-list")
    @ResponseBody
    public Map<String,Object> empList(Integer pageSize,Integer pageNum,String search){
        Page<Object> objects= PageHelper.startPage(pageSize,pageNum);
        List<User> users = empService.selectAllEmp(search);
        System.err.println("pageSize:"+pageSize);
        System.err.println("pageNum:"+pageNum);
        System.err.println("users:"+users);
        Map<String,Object> map=new HashMap<>();
        map.put("object",objects.getTotal());
        map.put("row", users);
        return map;
    }

    //查询所有部门
    @RequestMapping("/dept-list")
    @ResponseBody
    public List<Dept> deptList(){
        return empService.findAllDepts();
    }

    //新增
    @RequestMapping("/save-emp")
    @ResponseBody
    public void saveEmp(@RequestBody User user){
        empService.saveEmp(user);
    }

    //移除
    @RequestMapping("/yichu-emp")
    @ResponseBody
    public void yichuEmp(Integer empid){
        empService.yichuEmp(empid);
    }

    //根据部门id查看角色
    @RequestMapping("/emp-role")
    @ResponseBody
    public List<Role> roleAlls(Integer deptId){
        return empRoleService.roleAlls(deptId);
    }


    @RequestMapping("saveRole")
    @ResponseBody
    public void saveRole(Integer empId,String jsList){
        empRoleService.saveRole(empId,jsList);
    }

    @RequestMapping("xvniRoleSBSB")
    @ResponseBody
    public List<Role> xvniRoleSBSB(String jsList){
        return  empRoleService.xvniRoleSBSB(jsList);
    }

    //树形控件赋值查询
    @RequestMapping("tree")
    @ResponseBody
    public Map<String,Object> tree(){
        Map<String,Object> map = new HashMap<>();
        List<Dept> bm = empService.findAllDepts();
        List<List<User>> yg = new ArrayList<>();
        for (int i=0;i<bm.size();i++){
            System.err.println("findEmpByDept:"+bm.get(i).getDeptId());
            yg.add(empService.findEmpByDept(bm.get(i).getDeptId()));
        }
        map.put("bm",bm);
        map.put("yg",yg);
        return map;
    }

    //根据部门id查询此部门的信息
    @RequestMapping("selectDeptBydeptId")
    @ResponseBody
    public Dept selectDeptBydeptId(Integer deptId){
        return empService.selectDeptBydeptId(deptId);
    }

    //查看所有角色
    @RequestMapping("selectAllRoles")
    @ResponseBody
    public List<Role> selectAllRoles(){
        return empRoleService.selectAllRoles();
    }

    //根据部门id查看角色
    @RequestMapping("roleAllsBydeptId")
    @ResponseBody
    public List<Integer> roleAllsBydeptId(Integer deptId){
        return empRoleService.roleAllsBydeptId(deptId);
    }


    //确定对部门下的角色管理
    @RequestMapping("baocunBMGLjuese")
    @ResponseBody
    public void baocunBMGLjuese(Integer deptId,String jsList2){
         empRoleService.baocunBMGLjuese(deptId,jsList2);
    }

    //根据员工id查找角色-->empId
    @RequestMapping("roleAllsByempId")
    @ResponseBody
    public List<Integer> roleAllsByempId(Integer empId){
        return empRoleService.roleAllsByempId(empId);
    }


    //根据员工id查找员工信息
    @RequestMapping("selectEmpByEmpId")
    @ResponseBody
    public User selectEmpByEmpId(Integer empId){
        return empService.selectEmpByEmpId(empId);
    }


    //根据员工姓名来查询员工
    @RequestMapping("/selectEmpByEmpName")
    @ResponseBody
    public boolean selectEmpByEmpName(String empName){
        System.out.println("名字重复的的empName："+empName);
        User user = empService.selectEmpByEmpName(empName);
        System.out.println("名字重复的的："+user);
        if(user == null){
            return true;
        }else{
            return false;
        }
    }

    //根据员工身份证来查询员工
    @RequestMapping("/selectEmpByIdCard")
    @ResponseBody
    public boolean selectEmpByIdCard(String idCard){
        User user = empService.selectEmpByIdCard(idCard);
        if(user == null){
            return true;
        }else{
            return false;
        }
    }
    //根据员工电话来查询员工
    @RequestMapping("/selectEmpByEmpPhone")
    @ResponseBody
    public boolean selectEmpByEmpPhone(String empPhone){
        User user = empService.selectEmpByEmpPhone(empPhone);
        if(user == null){
            return true;
        }else{
            return false;
        }
    }

    //修改员工密码
    @RequestMapping("/updteEmpPasswordByEmpId")
    @ResponseBody
    public void updteEmpPasswordByEmpId(String empPwd, HttpSession session){
        User user = (User)session.getAttribute("user");
        empService.updteEmpPasswordByEmpId(MD5Util.string2MD5(empPwd),user.getEmpId());
    }


    @Autowired
    LoginService loginService;
    //判断原密码是否正确
    @RequestMapping("/yuanMM")
    @ResponseBody
    public String yuanMM(String empPwd, HttpSession session){
        User uesr = (User)session.getAttribute("user");
        User uesrOk = loginService.logingPD(uesr.getEmpName(),empPwd);
        if(uesrOk == null){
            return "400";
        }else {
            return "200";
        }
    }
}
